package bigdata.hourglass.engine.utils

import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.expressions.UserDefinedFunction

/**
 * @author leo.jie (weixiao.me@aliyun.com)
 * @organization DataReal
 * @version 1.0
 * @website https://www.jlpyyf.com
 * @date 2019/12/8 9:30 下午
 * @since 1.0
 */
object SparkUdf {
  /**
   * 根据正则过滤字符串
   * 不符合时可自定义返回值
   */
  def filterStrByFmtDefReturn(spark: SparkSession): UserDefinedFunction = {
    spark.udf.register("filterStrByFmtDefReturn", (s: String, reg: String, returnStr: String) => {
      if (s == null || (!s.matches(reg))) {
        returnStr
      } else {
        s.trim
      }
    })
  }

  /**
   * 字符串转数组
   */
  def str2array(spark: SparkSession): UserDefinedFunction = {
    spark.udf.register("str2array", (data: String) => {
      val array = com.alibaba.fastjson.JSON.parseArray(data)
      val value = array.iterator()
      var rList: List[String] = List()
      while (value.hasNext) {
        rList ::= value.next().toString
      }
      rList
    })
  }
}
